package cn.exrick.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.mapper.TbResourceCatMapper;
import cn.exrick.pojo.TbResourceCat;
import cn.exrick.pojo.TbResourceCatExample;
import cn.exrick.service.ResourceCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 */
@Service
public class ResourceCatServiceImpl implements ResourceCatService {

    @Autowired
    private TbResourceCatMapper tbResourceCatMapper;

    @Override
    public int save(TbResourceCat tbResourceCat) {
        if(getByName(tbResourceCat.getName())!=null){
            throw new XmallException("该分类名已存在");
        }
        tbResourceCat.setCreated(new Date());
        tbResourceCat.setUpdated(new Date());
        if(tbResourceCatMapper.insertSelective(tbResourceCat) != 1){
            throw new XmallException("添加资源分类失败");
        }
        return 1;
    }

    @Override
    public int update(TbResourceCat tbResourceCat) {
        if(!checkNameForEdit(tbResourceCat.getId(), tbResourceCat.getName())){
            throw new XmallException("该资源分类名已存在");
        }
        tbResourceCat.setUpdated(new Date());
        if(tbResourceCatMapper.updateByPrimaryKeySelective(tbResourceCat)!=1){
            throw new XmallException("更新资源分类失败");
        }
        return 1;
    }

    @Override
    public int delete(long id) {
        int count = tbResourceCatMapper.selectUsedCount(id);
        if(count != 0){
            return 0;
        }
        if(tbResourceCatMapper.deleteByPrimaryKey(id)!=1){
            throw new XmallException("删除资源分类失败");
        }
        return 1;
    }

    @Override
    public List<TbResourceCat> getAll() {
        TbResourceCatExample example = new TbResourceCatExample();
        List<TbResourceCat> list = tbResourceCatMapper.selectByExample(example);
        if(list == null){
            throw new XmallException("获取资源分类列表失败");
        }
        return list;
    }

    @Override
    public TbResourceCat getByName(String name) {
        TbResourceCatExample example = new TbResourceCatExample();
        TbResourceCatExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<TbResourceCat> list=new ArrayList<>();
        try {
            list = tbResourceCatMapper.selectByExample(example);
        }catch (Exception e){
            throw new XmallException("通过资源分类名获取分类失败");
        }
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean checkNameForEdit(long id, String name) {
        TbResourceCat tbResourceCat = tbResourceCatMapper.selectByPrimaryKey(id);
        TbResourceCat newResourceCat = null;
        if(tbResourceCat == null){
            throw new XmallException("通过ID获取资源分类失败");
        }
        if(!tbResourceCat.getName().equals(name)){
            newResourceCat = getByName(name);
        }
        return newResourceCat == null;
    }
    
}
